//
//  AllOrdersViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by LeGuo on 5/20/18.
//  Copyright © 2018 刘喆. All rights reserved.
//

#import "AllOrdersViewController.h"
#import "OrderDetailListTableViewCell.h"

@interface AllOrdersViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UIView *topContainerView;
@property(nonatomic, strong) UIView *middleContainerView;
@property(nonatomic, strong) UIView *bottomContainerView;

/** 订单时间 */
@property(nonatomic, strong) UILabel *orderTimeLabel;
/** 订单号 */
@property(nonatomic, strong) UILabel *orderNumberLabel;
/** 订单金额 */
@property(nonatomic, strong) UILabel *orderSumLabel;

/** 收货人 */
@property(nonatomic, strong) UILabel *consigneeLabel;
/** 收货地址 */
@property(nonatomic, strong) UILabel *consigneeAddressLabel;
/** 订单项 */
@property(nonatomic, strong) UITableView *orderDetailList;

/** 支付状态 */
@property(nonatomic, strong) UILabel *paymentStatusLabel;
/** 查看 */
@property(nonatomic, strong) UIButton *searchButton;
/** 删除 */
@property(nonatomic, strong) UIButton *deleteButton;
/** 继续支付 */
@property(nonatomic, strong) UIButton *continuePayingButton;

@end

@implementation AllOrdersViewController

#pragma mark - Cycle Life

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"订单内容";
    self.view.backgroundColor = UIColorWithRGB(245, 245, 245);
    
    [self loadLayout];
    
    self.orderTimeLabel.text = @"2018-05-03 13:25:58";
    self.orderNumberLabel.text = @"订单号:QDFRN2NOW0mgedeqUIVajAQ";
    self.orderSumLabel.text = @"订单金额: ¥48.00元";
    
    self.consigneeLabel.text = @"收货人:大家欧派";
    self.consigneeAddressLabel.text = @"收货地址:北京大兴区五环到六环之间 庆红沙发上几分离开静安寺发生了付款记录卡撒酒疯";
    
    self.paymentStatusLabel.text = @"未支付";
}

#pragma mark - Lazy Loading

- (UIView *)topContainerView {
    
    if (!_topContainerView) {
        
        _topContainerView = [[UIView alloc] init];
        _topContainerView.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:_topContainerView];
    }
    return _topContainerView;
}

- (UIView *)middleContainerView {
    
    if (!_middleContainerView) {
        
        _middleContainerView = [[UIView alloc] init];
        _middleContainerView.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:_middleContainerView];
    }
    return _middleContainerView;
}

- (UIView *)bottomContainerView {
    
    if (!_bottomContainerView) {
        
        _bottomContainerView = [[UIView alloc] init];
        _bottomContainerView.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:_bottomContainerView];
    }
    return _bottomContainerView;
}

- (UILabel *)orderTimeLabel {
    
    if (!_orderTimeLabel) {
        
        _orderTimeLabel = [[UILabel alloc] init];
        
        [self.topContainerView addSubview:_orderTimeLabel];
    }
    return _orderTimeLabel;
}

- (UILabel *)orderNumberLabel {
    
    if (!_orderNumberLabel) {
        
        _orderNumberLabel = [[UILabel alloc] init];
        
        [self.topContainerView addSubview:_orderNumberLabel];
    }
    return _orderNumberLabel;
}

- (UILabel *)orderSumLabel {
    
    if (!_orderSumLabel) {
        
        _orderSumLabel = [[UILabel alloc] init];
        
        [self.topContainerView addSubview:_orderSumLabel];
    }
    return _orderSumLabel;
}

- (UILabel *)consigneeLabel {
    
    if (!_consigneeLabel) {
        
        _consigneeLabel = [[UILabel alloc] init];
        
        [self.middleContainerView addSubview:_consigneeLabel];
    }
    return _consigneeLabel;
}

- (UILabel *)consigneeAddressLabel {
    
    if (!_consigneeAddressLabel) {
        
        _consigneeAddressLabel = [[UILabel alloc] init];
        _consigneeAddressLabel.numberOfLines = 2;
        
        [self.middleContainerView addSubview:_consigneeAddressLabel];
    }
    return _consigneeAddressLabel;
}

- (UITableView *)orderDetailList {
    
    if (!_orderDetailList) {
        
        _orderDetailList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _orderDetailList.backgroundColor = [UIColor clearColor];
        _orderDetailList.showsVerticalScrollIndicator = NO;
        _orderDetailList.showsHorizontalScrollIndicator = NO;
        _orderDetailList.separatorStyle = UITableViewCellSeparatorStyleNone;
        _orderDetailList.delegate = self;
        _orderDetailList.dataSource = self;
        [_orderDetailList registerNib:[UINib nibWithNibName:@"OrderDetailListTableViewCell" bundle:nil] forCellReuseIdentifier:@"orderListTableViewCell"];
        
        [self.view addSubview:_orderDetailList];
    }
    return _orderDetailList;
}

- (UILabel *)paymentStatusLabel {
    
    if (!_paymentStatusLabel) {
        
        _paymentStatusLabel = [[UILabel alloc] init];
        _paymentStatusLabel.font = [UIFont systemFontOfSize:12];
        _paymentStatusLabel.textColor = [UIColor grayColor];
        
        [self.bottomContainerView addSubview:_paymentStatusLabel];
    }
    return _paymentStatusLabel;
}

- (UIButton *)searchButton {
    
    if (!_searchButton) {
        
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchButton setTitle:@"查看" forState:UIControlStateNormal];
        [_searchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_searchButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [_searchButton addTarget:self action:@selector(searchButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomContainerView addSubview:_searchButton];
    }
    return _searchButton;
}

- (UIButton *)deleteButton {
    
    if (!_deleteButton) {
        
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_deleteButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [_deleteButton addTarget:self action:@selector(deleteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomContainerView addSubview:_deleteButton];
    }
    return _deleteButton;
}

- (UIButton *)continuePayingButton {
    
    if (!_continuePayingButton) {
        
        _continuePayingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _continuePayingButton.backgroundColor = [UIColor orangeColor];
        [_continuePayingButton setTitle:@"继续支付" forState:UIControlStateNormal];
        [_continuePayingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_continuePayingButton.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [_continuePayingButton addTarget:self action:@selector(continueButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomContainerView addSubview:_continuePayingButton];
    }
    return _continuePayingButton;
}

#pragma Layout

- (void)loadLayout {
    
    [self.topContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.left.and.right.equalTo(self.view);
    }];
    
    [self.middleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topContainerView.mas_bottom).offset(20);
        make.left.and.right.equalTo(self.view);
    }];
    
    [self.bottomContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.and.right.equalTo(self.view);
        make.height.mas_equalTo(@49);
    }];
    
    [self.orderTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topContainerView).offset(8);
        make.left.equalTo(self.topContainerView).offset(12);
        make.right.equalTo(self.topContainerView).offset(-12);
    }];
    
    [self.orderNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderTimeLabel.mas_bottom).offset(5);
        make.left.and.right.equalTo(self.orderTimeLabel);
    }];
    
    [self.orderSumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderNumberLabel.mas_bottom).offset(5);
        make.left.and.right.equalTo(self.orderNumberLabel);
    }];
    
    [self.topContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.orderSumLabel).offset(8);
    }];
    
    [self.consigneeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.middleContainerView).offset(8);
        make.left.equalTo(self.middleContainerView).offset(12);
        make.right.mas_equalTo(self.middleContainerView).offset(-12);
    }];
    
    [self.consigneeAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.consigneeLabel.mas_bottom).offset(5);
        make.left.and.right.equalTo(self.consigneeLabel);
    }];
    
    [self.middleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.consigneeAddressLabel.mas_bottom).offset(8);
    }];
    
    [self.paymentStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomContainerView).offset(12);
        make.centerY.equalTo(self.bottomContainerView);
    }];
    
    [self.continuePayingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.and.right.equalTo(self.bottomContainerView);
        make.width.mas_equalTo(@80);
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.continuePayingButton.mas_left).offset(-10);
        make.centerY.equalTo(self.bottomContainerView);
        make.width.mas_equalTo(@50);
    }];
    
    UIView *separatorLine = [[UIView alloc] init];
    separatorLine.backgroundColor = [UIColor blackColor];
    [self.bottomContainerView addSubview:separatorLine];
    [separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.deleteButton.mas_left).offset(5);
        make.centerY.equalTo(self.bottomContainerView);
        make.width.mas_equalTo(@1);
        make.height.mas_equalTo(@15);
    }];
    
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(separatorLine.mas_left).offset(5);
        make.centerY.equalTo(self.bottomContainerView);
        make.width.equalTo(@50);
    }];
    
    [self.orderDetailList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.middleContainerView.mas_bottom).offset(20);
        make.left.and.right.equalTo(self.view);
        make.bottom.equalTo(self.bottomContainerView.mas_top);
    }];
}

#pragma mark - UITableViewDelegate && UITableViewDatasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reusedIdentifier = @"orderListTableViewCell";
    
    OrderDetailListTableViewCell *orderDetailCell = [tableView dequeueReusableCellWithIdentifier:reusedIdentifier forIndexPath:indexPath];
    
    orderDetailCell.imageView.backgroundColor = [UIColor yellowColor];
    orderDetailCell.commodityNameLabel.text = @"<<大学生>>2018全年订阅";
    orderDetailCell.commoditySumLabel.text = @"¥0.00";
    
    return orderDetailCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0f;
}

#pragma mark - Button Action

- (void)searchButtonClicked:(UIButton *)sender {
    
}

- (void)deleteButtonClicked:(UIButton *)sender {
    
}

- (void)continueButtonClicked:(UIButton *)sender {
    
}

#pragma mark - Memory Warning

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
